#include "task.h"
#include "client.h"
#include "device.h"

void Task::upload() {
    // 写入任务 0x43+1(设备编号)+8(任务ID)+1(是否启用)+1(优先级)+1(是否重复)+1(任务类型)+1(采样电流档位)+2(采样频率)+8(任务间隔时间)
    QByteArray data;
    data.append(0x43);
    data.append(static_cast<char>(qobject_cast<Device *>(parent())->device_id()));
    data.append(reinterpret_cast<const char *>(&id), sizeof(id));
    data.append(static_cast<char>(is_enabled ? 1 : 0));
    data.append(static_cast<char>(priority));
    data.append(static_cast<char>(repeat ? 1 : 0));
    data.append(static_cast<char>(taskType));
    data.append(static_cast<char>(current));
    data.append(reinterpret_cast<const char *>(&freq), sizeof(freq));
    data.append(reinterpret_cast<const char *>(&interval), sizeof(interval));
    // 发送数据到服务器
    qobject_cast<Client *>(qobject_cast<Device *>(parent())->parent())->sendToServer(data);
    qDebug() << "Task uploaded with ID:" << id
             << "Enabled:" << is_enabled
             << "Priority:" << static_cast<int>(priority)
             << "Repeat:" << repeat
             << "Task Type:" << static_cast<int>(taskType)
             << "Current:" << static_cast<int>(current)
             << "Frequency:" << freq
             << "Interval:" << interval;
}
